﻿using System.Data;
using System.Linq;
using System.Web.Mvc;
using Demo.Model;
using Demo.MvcApp.BootstrapSupport;
using Demo.MvcApp.Models;
using OperationPlugins;
using OperationPlugins.Mvc;
using OperationPlugins.Mvc.Filters;

namespace Demo.MvcApp.Controllers
{
    [CurrentBranch("bbb")]
    [UsePluginHandlersForAllActions(ActionMethodMoment.OnActionExecuted)]
    [NotificationRouterForAllActions]
    public class DemoController : Controller
    {
        private readonly DemoContext _db = new DemoContext();

        /// <summary>
        /// GET: /Demo/ 
        /// </summary>
        public ActionResult Index()
        {
            return View(_db.Employees.ToList());
        }

        /// <summary>
        /// GET: /Demo/Create 
        /// </summary>
        public ActionResult Create()
        {
            return View();
        }

        /// <summary>
        /// POST: /Demo/Create 
        /// </summary>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Employee employee)
        {
            if (ModelState.IsValid)
            {
                _db.Employees.Add(employee);
                _db.SaveChanges();

                return RedirectToAction("Index");
            }
            return View(employee);
        }

        /// <summary>
        /// GET: /Demo/Edit/5 
        /// </summary>
        public ActionResult Edit(int id = 0)
        {
            Employee employee = _db.Employees.Find(id);
            if (employee == null)
            {
                return HttpNotFound();
            }
            return View(employee);
        }

        /// <summary>
        /// POST: /Demo/Edit/5 
        /// </summary>
        [HttpPost]
        [ValidateAntiForgeryToken]
        [CurrentBranch("bbb")]
        [UsePluginHandlers(ActionMethodMoment.OnActionExecuted)]
        public ActionResult Edit(Employee employee)
        {
            //using (new ValidationScope(this))
            {
                if (ModelState.IsValid)
                {
                    _db.Entry(employee).State = EntityState.Modified;
                    _db.SaveChanges();
                    return RedirectToAction("Index");
                }
                return View(employee);
            }
        }

        /// <summary>
        /// GET: /Demo/Delete/5 
        /// </summary>
        public ActionResult Delete(int id = 0)
        {
            Employee employee = _db.Employees.Find(id);
            if (employee == null)
            {
                return HttpNotFound();
            }
            return View(employee);
        }

        /// <summary>
        /// POST: /Demo/Delete/5 
        /// </summary>
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Employee employee = _db.Employees.Find(id);
            _db.Employees.Remove(employee);
            _db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            _db.Dispose();
            base.Dispose(disposing);
        }
    }
}